

#include "mpi_lib.h"
#include "sample_venc.h"
#include "mdi_common.h"
#include "lcd_common.h"
#include <sys/time.h>
#include "ringbuffer.hpp"
//#define PRINT_DEBUG_LOG
//#define ENCODE_PARAM_TEST
#define ENCODE_SAVE_FILE
#define SAVE_H264_PER_MINUTE
//#define ENABLE_VI_SWITCH_TESE
#define AUTO_DELETE_H264_FILE

#define SAVE_FILE_TIME            (60*5) /*second*/
#define TEST_TIME_INTERVAL        10   /*second*/
#define START_STOP_TEST_NUM       1000

static video_encode_stream_s gVEncStreams[VENC_STREAM_NUM];
pthread_mutex_t gLock;
struct inparam inputparam;

int g_save_h264_file_flag = 0;

static sgks_mpi_dsp_status_s   g_dsp_state;
static struct timeval enc_pre_tv;
static struct timeval start_tv[VENC_STREAM_NUM], stop_tv[VENC_STREAM_NUM];

int sgks_mpi_xc9080_sample_venc_init()
{
    int ret = SGKS_SUCCESS;

    g_dsp_state.op_mode = SGKS_DSP_UNKNOWN_MODE;
    g_dsp_state.op_sub_mode = SGKS_DSP_ENC_UNKNOWN_MODE;
    g_dsp_state.state = SGKS_ENC_UNKNOWN_STATE;

    ret = sgks_mpi_venc_init();
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: venc init fail\n", __FUNCTION__));
    }
    return ret;
}

int sgks_mpi_xc9080_sample_venc_param_setting(int venc_test_mode)
{
    int ret = SGKS_SUCCESS;
    sgks_mpi_venc_channel_param_s chansParams;

    gVEncStreams[0].streamFormat.streamId = 0;
    gVEncStreams[0].streamFormat.encodeType = 1;
    if((venc_test_mode == YUV_DATA_H264_ENCODE_TEST) || (venc_test_mode == YUV_DATA_MJPEG_ENCODE_TEST))
    {
        gVEncStreams[0].streamFormat.width = 1280;
        gVEncStreams[0].streamFormat.height = 720;
    }
    else
    {
        gVEncStreams[0].streamFormat.width = 1920;
        gVEncStreams[0].streamFormat.height = 1080;
    }
    gVEncStreams[0].streamFormat.fps = VENC_FPS_25;
    gVEncStreams[0].streamFormat.flipRotate = 0;
    gVEncStreams[0].streamFormat.keepAspRat = 1;
    gVEncStreams[0].h264Conf.streamId = 0;
    gVEncStreams[0].h264Conf.gopModel = 0;
    gVEncStreams[0].h264Conf.gopM = 1;
    gVEncStreams[0].h264Conf.gopN = 25;
    gVEncStreams[0].h264Conf.idrInterval = 1;
    gVEncStreams[0].h264Conf.profile = 0;
    gVEncStreams[0].h264Conf.brcMode = 0;
    gVEncStreams[0].h264Conf.cbrAvgBps = 8000000;
    gVEncStreams[0].h264Conf.vbrMinbps = 1000000;
    gVEncStreams[0].h264Conf.vbrMaxbps = 4000000;
    gVEncStreams[0].h264Conf.adaptQp = 2;
    gVEncStreams[0].h264Conf.qpMinI = 20;
    gVEncStreams[0].h264Conf.qpMinP = 23;
    gVEncStreams[0].h264Conf.qpIWeight = 1;
    gVEncStreams[0].h264Conf.qpPWeight = 5;
    gVEncStreams[0].mjpegConf.streamId  = 0;
    gVEncStreams[0].mjpegConf.quality = 50;
    gVEncStreams[0].mjpegConf.chromaFormat = 1;

    gVEncStreams[1].streamFormat.streamId = 1;
    gVEncStreams[1].streamFormat.encodeType = 0;
	gVEncStreams[2].streamFormat.width = 1280;
    gVEncStreams[2].streamFormat.height = 720;
    gVEncStreams[1].streamFormat.fps = VENC_FPS_25;
    gVEncStreams[1].streamFormat.flipRotate = 0;
    gVEncStreams[1].streamFormat.keepAspRat = 1;
    gVEncStreams[1].h264Conf.streamId = 1;
    gVEncStreams[1].h264Conf.gopModel = 0;
    gVEncStreams[1].h264Conf.gopM = 1;
    gVEncStreams[1].h264Conf.gopN = 25;
    gVEncStreams[1].h264Conf.idrInterval = 1;
    gVEncStreams[1].h264Conf.profile = 0;
    gVEncStreams[1].h264Conf.brcMode = 0;
    gVEncStreams[1].h264Conf.cbrAvgBps = 2000000;
    gVEncStreams[1].h264Conf.vbrMinbps = 1000000;
    gVEncStreams[1].h264Conf.vbrMaxbps = 2000000;
    gVEncStreams[1].h264Conf.adaptQp = 2;
    gVEncStreams[1].h264Conf.qpMinI = 20;
    gVEncStreams[1].h264Conf.qpMinP = 23;
    gVEncStreams[1].h264Conf.qpIWeight = 1;
    gVEncStreams[1].h264Conf.qpPWeight = 5;
    gVEncStreams[1].mjpegConf.streamId  = 1;
    gVEncStreams[1].mjpegConf.quality = 50;
    gVEncStreams[1].mjpegConf.chromaFormat = 1;

    gVEncStreams[2].streamFormat.streamId = 2;
    gVEncStreams[2].streamFormat.encodeType = 0;
    gVEncStreams[2].streamFormat.width = 1280;
    gVEncStreams[2].streamFormat.height = 720;
    gVEncStreams[2].streamFormat.fps = VENC_FPS_25;
    gVEncStreams[2].streamFormat.flipRotate = 0;
    gVEncStreams[2].streamFormat.keepAspRat = 1;
    gVEncStreams[2].h264Conf.streamId = 2;
    gVEncStreams[2].h264Conf.gopModel = 0;
    gVEncStreams[2].h264Conf.gopM = 1;
    gVEncStreams[2].h264Conf.gopN = 25;
    gVEncStreams[2].h264Conf.idrInterval = 1;
    gVEncStreams[2].h264Conf.profile = 0;
    gVEncStreams[2].h264Conf.brcMode = 0;
    gVEncStreams[2].h264Conf.cbrAvgBps = 50000;
    gVEncStreams[2].h264Conf.vbrMinbps = 10000;
    gVEncStreams[2].h264Conf.vbrMaxbps = 100000;
    gVEncStreams[2].h264Conf.adaptQp = 2;
    gVEncStreams[2].h264Conf.qpMinI = 20;
    gVEncStreams[2].h264Conf.qpMinP = 23;
    gVEncStreams[2].h264Conf.qpIWeight = 1;
    gVEncStreams[2].h264Conf.qpPWeight = 5;
    gVEncStreams[2].mjpegConf.streamId  = 2;
    gVEncStreams[2].mjpegConf.quality = 50;
    gVEncStreams[2].mjpegConf.chromaFormat = 1;



    gVEncStreams[3].streamFormat.streamId = 3;
    gVEncStreams[3].streamFormat.encodeType = 0;
    gVEncStreams[3].streamFormat.width = 1280;
    gVEncStreams[3].streamFormat.height = 720;
    gVEncStreams[3].streamFormat.fps = VENC_FPS_3;
    gVEncStreams[3].streamFormat.flipRotate = 0;
    gVEncStreams[3].streamFormat.keepAspRat = 1;
    gVEncStreams[3].h264Conf.streamId = 3;
    gVEncStreams[3].h264Conf.gopModel = 0;
    gVEncStreams[3].h264Conf.gopM = 1;
    gVEncStreams[3].h264Conf.gopN = 50;
    gVEncStreams[3].h264Conf.idrInterval = 1;
    gVEncStreams[3].h264Conf.profile = 0;
    gVEncStreams[3].h264Conf.brcMode = 0;
    gVEncStreams[3].h264Conf.cbrAvgBps = 500000;
    gVEncStreams[3].h264Conf.vbrMinbps = 500000;
    gVEncStreams[3].h264Conf.vbrMaxbps = 1000000;
    gVEncStreams[3].h264Conf.adaptQp = 2;
    gVEncStreams[3].h264Conf.qpMinI = 20;
    gVEncStreams[3].h264Conf.qpMinP = 23;
    gVEncStreams[3].h264Conf.qpIWeight = 1;
    gVEncStreams[3].h264Conf.qpPWeight = 5;
    gVEncStreams[3].mjpegConf.streamId  = 3;
    gVEncStreams[3].mjpegConf.quality = 50;
    gVEncStreams[3].mjpegConf.chromaFormat = 1;

    ret = sgks_mpi_venc_get_params(VENC_PARAM_TYPE_CHANNEL, (void *)&chansParams);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("Get Channel Params Fail\n"));
    }
    int ch = 0;
    sgks_mpi_venc_stream_format_param_s *streamFormat;
    for (ch = 0; ch < VENC_STREAM_NUM; ch++)
    {
        streamFormat = &(gVEncStreams[ch].streamFormat);
        streamFormat->channelId = streamFormat->streamId;
        switch(streamFormat->streamId)
        {
            case VENC_STREAM_FIRST:
            {
                //main channel
                if(streamFormat->encodeType != SGKS_ENCODE_NONE)
                {
                    chansParams.chan1Type = VENC_CHANNEL_TYPE_ENCODE;
                }
                else
                {
                    chansParams.chan1Type = VENC_CHANNEL_TYPE_OFF;
                }
                chansParams.chan1Width = streamFormat->width;
                chansParams.chan1Height = streamFormat->height;
                chansParams.intlcScan = 0;
                break;
            }
            case VENC_STREAM_SECOND:
            {
                //preview_c
                if(streamFormat->encodeType != SGKS_ENCODE_NONE)
                {
                    chansParams.chan2Type = VENC_CHANNEL_TYPE_ENCODE;
                }
                else
                {
                    chansParams.chan2Type = VENC_CHANNEL_TYPE_OFF;
                }
                chansParams.chan2Width = streamFormat->width;
                chansParams.chan2Height = streamFormat->height;
                break;
            }
            case VENC_STREAM_THIRD:
            {
                //preview_b
                if(streamFormat->encodeType != SGKS_ENCODE_NONE)
                {
                    chansParams.chan3Type = VENC_CHANNEL_TYPE_OFF;
                }
                else
                {
                    chansParams.chan3Type   = VENC_CHANNEL_TYPE_OFF;
                }
                chansParams.chan3Width  = streamFormat->width;
                chansParams.chan3Height = streamFormat->height;
                break;
            }
            case VENC_STREAM_FORTH:
            {
                //preview_a
                if(streamFormat->encodeType != SGKS_ENCODE_NONE)
                {
                    chansParams.chan4Type = VENC_CHANNEL_TYPE_OFF;
                }
                else
                {
                    chansParams.chan4Type   = VENC_CHANNEL_TYPE_PREVIEW;
                }
                chansParams.chan4Width  = streamFormat->width;
                chansParams.chan4Height = streamFormat->height;
                break;
            }
            default:
            {
                DEBUGMSG(ZONE_ERROR, ("Error: %s(%d): streamId:%d\n", __func__, __LINE__, streamFormat->streamId));
                break;
            }
        }
    }

    ret = sgks_mpi_venc_set_params(VENC_PARAM_TYPE_CHANNEL, (void *)&chansParams);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("Set Channel Params Fail\n"));
        return ret;
    }

    int cnt = 0;
    u8  encodeType;
    for (cnt = 0; cnt < VENC_STREAM_NUM; cnt++)
    {
        encodeType = gVEncStreams[cnt].streamFormat.encodeType;
        if(encodeType != SGKS_ENCODE_NONE)
        {
            ret = sgks_mpi_venc_set_params(VENC_PARAM_TYPE_STREAM, (void *)&gVEncStreams[cnt].streamFormat);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("Set Stream Format Fail\n"));
                return ret;
            }

            if(encodeType == SGKS_ENCODE_H264)
            {
                ret = sgks_mpi_venc_set_config(VENC_CONFIG_TYPE_H264, (void *)&gVEncStreams[cnt].h264Conf);
                if(ret != SGKS_SUCCESS)
                {
                    DEBUGMSG(ZONE_ERROR, ("Set H264 Config Fail\n"));
                    return ret;
                }
            }

            if(encodeType == SGKS_ENCODE_MJPEG)
            {
                ret = sgks_mpi_venc_set_config(VENC_CONFIG_TYPE_MJPEG, (void *)&gVEncStreams[cnt].mjpegConf);
                if(ret != SGKS_SUCCESS)
                {
                    DEBUGMSG(ZONE_ERROR, ("Set MJPEG Config Fail\n"));
                    return ret;
                }
            }
        }
    }

    return ret;
}

int sgks_mpi_xc9080_sample_venc_setting(int venc_test_mode)
{
    int ret = SGKS_SUCCESS;

    ret = sgks_mpi_xc9080_sample_venc_init();
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: venc init fail\n", __FUNCTION__));
        return ret;
    }

    ret = sgks_mpi_xc9080_sample_venc_param_setting(venc_test_mode);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: venc init fail\n", __FUNCTION__));
        return ret;
    }
    return ret;
}

int sgks_mpi_xc9080_sample_venc_start()
{
    int ret = SGKS_SUCCESS;
    u32 cnt;
    for (cnt = 0; cnt < VENC_STREAM_FORTH; cnt++)
    {
        if(gVEncStreams[cnt].streamFormat.encodeType != 0)
        {
            ret = sgks_mpi_venc_state_control(VENC_STATE_CONTROL_START, cnt);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("%s: venc state control fail\n", __FUNCTION__));
                return ret;
            }
            gettimeofday(&start_tv[cnt], NULL);
        }
    }
    return ret;
}


int sgks_mpi_xc9080_sample_venc_stop()
{
    int ret = SGKS_SUCCESS;
    u32 cnt;
    for (cnt = 0; cnt < VENC_STREAM_FORTH; cnt++)
    {
        if(gVEncStreams[cnt].streamFormat.encodeType != 0)
        {
            ret = sgks_mpi_venc_state_control(VENC_STATE_CONTROL_STOP, cnt);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("%s: venc state control(stop) fail\n", __FUNCTION__));
                return ret;
            }
            gettimeofday(&stop_tv[cnt], NULL);
        }
    }
    return ret;
}

static int venc_sample_get_dsp_encode_state(sgks_mpi_dsp_status_s *dsp_encode_state)
{
    int ret = SGKS_SUCCESS;

    sgks_mpi_dsp_status_s   dsp_state;
    dsp_state.op_mode = SGKS_DSP_UNKNOWN_MODE;
    dsp_state.op_sub_mode = SGKS_DSP_ENC_UNKNOWN_MODE;
    dsp_state.state = SGKS_ENC_UNKNOWN_STATE;

    ret = sgks_mpi_sys_GetDspStats(&dsp_state);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("Get DSP STATE FAIL\n"));
        ret = SGKS_ERR_VENC_DEVICEERR;
    }
    memcpy(dsp_encode_state, &dsp_state, sizeof(sgks_mpi_dsp_status_s));
    return ret;
}

static int venc_sample_save_encode(sgks_encode_stream_s *pEncodeStream)
{
    static int once[4] = {0};
    static int fd_sav[4];
    static char openfile[4][40], filedir[30];
    static char deletefile[40];
    frame_head_s frame_head;
    struct timeval cur_tv;
    static int file_order[4] = {0};
    u32 time_sec = 0;
    static int flag = 1;
	struct timeval tv1, tv2;

    memset(&frame_head, 0x00, sizeof(frame_head_s));
    if(pEncodeStream->vetEncodeType == vtH264)
    {
        static int mkdironce = 0;
        if(mkdironce  == 0)
        {
            mkdironce = 1;
			system("rm -rf /home/TF/H264");
			umask(~(S_IRWXU|S_IRWXO|S_IRWXG));
		    system("mkdir -p /home/TF/H264");
		    strcpy(filedir, "/home/TF/H264");	
        }
#if 0
        gettimeofday(&cur_tv, NULL);
        time_sec = cur_tv.tv_sec - enc_pre_tv.tv_sec + 1;
        if(time_sec%SAVE_FILE_TIME == 0)
        {
            if(flag == 0)
            {
                once[0] = 0;
                once[1] = 0;
                once[2] = 0;
                once[3] = 0;
                flag = 1;
            }
        }
        else
        {
            flag = 0;
        }
#endif
        if(once[pEncodeStream->nChannel] == 0)
        {
            sprintf(openfile[pEncodeStream->nChannel], "%s/h264_ch%d_%d.h264", filedir, pEncodeStream->nChannel, file_order[pEncodeStream->nChannel]);
#ifdef AUTO_DELETE_H264_FILE
            if(file_order[pEncodeStream->nChannel] > 0)
            {
                sprintf(deletefile, "rm -rf %s/h264_ch%d_%d.h264", filedir, pEncodeStream->nChannel, file_order[pEncodeStream->nChannel] - 1);
                close(fd_sav[pEncodeStream->nChannel]);
                system(deletefile);
            }
#endif            
            fd_sav[pEncodeStream->nChannel] = open(openfile[pEncodeStream->nChannel], O_CREAT|O_RDWR, (S_IRWXU|S_IRWXO|S_IRWXG));
            if(fd_sav[pEncodeStream->nChannel] < 0)
            {
                DEBUGMSG(ZONE_ERROR, ("open file:%s error:%d\n", openfile[pEncodeStream->nChannel], fd_sav[pEncodeStream->nChannel]));
            }
            once[pEncodeStream->nChannel] = 1;
            file_order[pEncodeStream->nChannel] +=1;
            DEBUGMSG(ZONE_VENC, ("open %s file ok\n", openfile[pEncodeStream->nChannel]));
        }

        frame_head.ch = pEncodeStream->nChannel;
        if(pEncodeStream->nFrameType == 1)
        {
            frame_head.frame_type = 1;
        }
        else if(pEncodeStream->nFrameType == 3)
        {
            frame_head.frame_type = 2;
        }
        else
        {
            DEBUGMSG(ZONE_VENC, ("h264 frame type error(%d)\n", pEncodeStream->nFrameType));
        }
        frame_head.frame_size=pEncodeStream->nSize;
        frame_head.frame_no =pEncodeStream->FrameNumber;
        frame_head.reserve[0] = 0x12;
        frame_head.reserve[1] = 0x34;
        frame_head.reserve[2] = 0x56;
        frame_head.reserve[3] = 0x78;
		//Printf("pEncodeStream->pData 0x%x\n",pEncodeStream->pData[4]);

		#if 1
		//gettimeofday(&tv1, NULL);
        write(fd_sav[pEncodeStream->nChannel], &frame_head, sizeof(frame_head_s)); 
        write(fd_sav[pEncodeStream->nChannel], pEncodeStream->pData, pEncodeStream->nSize);
        //fsync(fd_sav[pEncodeStream->nChannel]);
		//gettimeofday(&tv2, NULL);
        //DEBUGMSG(ZONE_VENC, ("****w****time: %dms\n", tv2.tv_sec*1000 + tv2.tv_usec/1000 - tv1.tv_sec*1000 - tv1.tv_usec/1000));
#else
		sleep(1);
		#endif
    }
    else
    {
        //if(once[pEncodeStream->nChannel] == 0)
        {
            static int mkdironce = 0;
            static unsigned int filecount = 0;
            if(mkdironce  == 0)
            {
                mkdironce = 1;
                system("rm -rf JPEG");
                umask(~(S_IRWXU|S_IRWXO|S_IRWXG));
                system("mkdir JPEG");
                strcpy(filedir, "JPEG");
            }
            strcpy(filedir, "JPEG");
            sprintf(openfile[pEncodeStream->nChannel], "%s/jpeg_ch%d_%d.jpeg", filedir, pEncodeStream->nChannel, filecount++);
            fd_sav[pEncodeStream->nChannel] = open(openfile[pEncodeStream->nChannel], O_CREAT|O_RDWR, (S_IRWXU|S_IRWXO|S_IRWXG));
            if(fd_sav[pEncodeStream->nChannel] < 0)
            {
                DEBUGMSG(ZONE_ERROR, ("open file:%s error:%d\n", openfile[pEncodeStream->nChannel], fd_sav));
            }
            DEBUGMSG(ZONE_INFO, ("open file:%s ok\n", openfile[pEncodeStream->nChannel]));
            //once[pEncodeStream->nChannel] = 1;
            write(fd_sav[pEncodeStream->nChannel], pEncodeStream->pData, pEncodeStream->nSize);
            close(fd_sav[pEncodeStream->nChannel]);
        }

    }

    return SGKS_SUCCESS;
}

static void venc_sample_video_encode_callback(sgks_encode_stream_s *pEncodeStream)
{
    if (pEncodeStream != NULL)
    {
        switch(pEncodeStream->stType)
        {
            case stmVideo:
            {
                venc_sample_save_encode(pEncodeStream);
                break;
            }
        }
    }
}


int sgks_mpi_sample_venc_param_test(void)
{
    int ret = SGKS_SUCCESS;

    sgks_mpi_venc_stream_format_param_s  *pStreamFormatParam;
    pStreamFormatParam = (sgks_mpi_venc_stream_format_param_s  *)sgks_mpi_Malloc(sizeof(sgks_mpi_venc_stream_format_param_s));
    memset(pStreamFormatParam, 0x00, sizeof(sgks_mpi_venc_stream_format_param_s));
    pStreamFormatParam->streamId = 0;

    ret = sgks_mpi_venc_get_params(VENC_PARAM_TYPE_STREAM, (void *)pStreamFormatParam);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: get stream params fail\n", __FUNCTION__));
        return ret;
    }
#ifdef PRINT_DEBUG_LOG
    DEBUGMSG(ZONE_INFO, ("StreamParam: streamId=%d\n", pStreamFormatParam->streamId));
    DEBUGMSG(ZONE_INFO, ("StreamParam: encodeType=%d\n", pStreamFormatParam->encodeType));
    DEBUGMSG(ZONE_INFO, ("StreamParam: channelId=%d\n", pStreamFormatParam->channelId));
    DEBUGMSG(ZONE_INFO, ("StreamParam: width=%d\n", pStreamFormatParam->width));
    DEBUGMSG(ZONE_INFO, ("StreamParam: height=%d\n", pStreamFormatParam->height));
    DEBUGMSG(ZONE_INFO, ("StreamParam: xOffset=%d\n", pStreamFormatParam->xOffset));
    DEBUGMSG(ZONE_INFO, ("StreamParam: yOffset=%d\n", pStreamFormatParam->yOffset));
    DEBUGMSG(ZONE_INFO, ("StreamParam: fps=%d\n", pStreamFormatParam->fps));
    DEBUGMSG(ZONE_INFO, ("StreamParam: keepAspRat=%d\n", pStreamFormatParam->keepAspRat));
#endif
    if(pStreamFormatParam->height == 1080)
    {
        pStreamFormatParam->width = 1280;
        pStreamFormatParam->height = 720;
        pStreamFormatParam->fps = VENC_FPS_20;
    }
    else
    {
        pStreamFormatParam->width = 1920;
        pStreamFormatParam->height = 1080;
        pStreamFormatParam->fps = VENC_FPS_25;
    }
    ret = sgks_mpi_venc_set_params(VENC_PARAM_TYPE_STREAM, (void *)pStreamFormatParam);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: set stream params fail\n", __FUNCTION__));
        return ret;
    }


    sgks_mpi_venc_h264_config_s *pH264Config = NULL;
    pH264Config = (sgks_mpi_venc_h264_config_s *)sgks_mpi_Malloc(sizeof(sgks_mpi_venc_h264_config_s));
    memset(pH264Config, 0x00, sizeof(sgks_mpi_venc_h264_config_s));
    pH264Config->streamId = 0;

    ret = sgks_mpi_venc_get_config(VENC_CONFIG_TYPE_H264, (void *)pH264Config);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: get h264 config params fail\n", __FUNCTION__));
        return ret;
    }
#ifdef PRINT_DEBUG_LOG
    DEBUGMSG(ZONE_INFO, ("H264Config: streamId=%d\n", pH264Config->streamId));
    DEBUGMSG(ZONE_INFO, ("H264Config: gopM=%d\n", pH264Config->gopM));
    DEBUGMSG(ZONE_INFO, ("H264Config: gopN=%d\n", pH264Config->gopN));
    DEBUGMSG(ZONE_INFO, ("H264Config: idrInterval=%d\n", pH264Config->idrInterval));
    DEBUGMSG(ZONE_INFO, ("H264Config: gopModel=%d\n", pH264Config->gopModel));
    DEBUGMSG(ZONE_INFO, ("H264Config: profile=%d\n", pH264Config->profile));
    DEBUGMSG(ZONE_INFO, ("H264Config: brcMode=%d\n", pH264Config->brcMode));
    DEBUGMSG(ZONE_INFO, ("H264Config: cbrAvgBps=%d\n", pH264Config->cbrAvgBps));
    DEBUGMSG(ZONE_INFO, ("H264Config: vbrMinbps=%d\n", pH264Config->vbrMinbps));
    DEBUGMSG(ZONE_INFO, ("H264Config: vbrMaxbps=%d\n", pH264Config->vbrMaxbps));
    DEBUGMSG(ZONE_INFO, ("H264Config: adaptQp=%d\n", pH264Config->adaptQp));
    DEBUGMSG(ZONE_INFO, ("H264Config: qcon=%d\n", pH264Config->qcon));
    DEBUGMSG(ZONE_INFO, ("H264Config: qpMinI=%d\n", pH264Config->qpMinI));
    DEBUGMSG(ZONE_INFO, ("H264Config: qpMinP=%d\n", pH264Config->qpMinP));
    DEBUGMSG(ZONE_INFO, ("H264Config: qpIWeight=%d\n", pH264Config->qpIWeight));
    DEBUGMSG(ZONE_INFO, ("H264Config: qpPWeight=%d\n", pH264Config->qpPWeight));
#endif
    pH264Config->gopN = 23;
    pH264Config->brcMode = 0;
    pH264Config->cbrAvgBps = 10000000;
    pH264Config->vbrMinbps = 1000000;
    pH264Config->vbrMaxbps = 6000000;
    ret = sgks_mpi_venc_set_config(VENC_CONFIG_TYPE_H264, (void *)pH264Config);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: get h264 config params fail\n", __FUNCTION__));
        return ret;
    }
    if(ret == SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_INFO, ("%s end\n", __FUNCTION__));
    }
    return ret;
}

static void *venc_sample_pressure_test_proccess(void *params)
{
    int ret = SGKS_SUCCESS;
    u32 cur_venc_state = 0xFF;
    struct timeval cur_tv;
    int second_time;
    u32 test_time = 0;
    int strema_0_stop_flag = 0;
    int strema_0_start_flag = 0;
    int strema_1_stop_flag = 0;
    int strema_1_start_flag = 0;
    sgks_mpi_venc_stream_s *stream = (sgks_mpi_venc_stream_s *)params;

    pthread_detach(pthread_self());
    if(params == NULL)
    {
        ret = -1;
        pthread_exit((int *)ret);
    }
    do
    {
        int stream_id = 0;
        if(gVEncStreams[stream_id].streamFormat.encodeType != 0)
        {
            ret = venc_sample_get_dsp_encode_state(&g_dsp_state);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("%s: venc get dsp state fail\n", __FUNCTION__));
                break;
            }
            if((strema_0_stop_flag == 0) && (g_dsp_state.stream_encode_state[stream_id] == 1))
            {
                DEBUGMSG(ZONE_VENC, ("++stream %d stop++\n", stream_id));
                ret = sgks_mpi_venc_state_control(VENC_STATE_CONTROL_STOP, stream_id);
                if(ret != SGKS_SUCCESS)
                {
                    DEBUGMSG(ZONE_ERROR, ("%s: venc state control(stop) fail\n", __FUNCTION__));
                    break;
                }
                do
                {
                    ret = venc_sample_get_dsp_encode_state(&g_dsp_state);
                    if(ret != SGKS_SUCCESS)
                    {
                        DEBUGMSG(ZONE_ERROR, ("%s: venc get dsp state fail\n", __FUNCTION__));
                        break;
                    }
                }
                while(g_dsp_state.stream_encode_state[stream_id] == 1);
                DEBUGMSG(ZONE_VENC, ("--stream %d stop--\n\n", stream_id));
                strema_0_stop_flag = 1;
                strema_0_start_flag = 0;
            }

            ret = venc_sample_get_dsp_encode_state(&g_dsp_state);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("%s: venc get dsp state fail\n", __FUNCTION__));
                break;
            }
            if((strema_0_start_flag == 0) && (g_dsp_state.stream_encode_state[stream_id] == 0))
            {
                DEBUGMSG(ZONE_VENC, ("++stream %d start++\n", stream_id));
                ret = sgks_mpi_venc_state_control(VENC_STATE_CONTROL_START, stream_id);
                if(ret != SGKS_SUCCESS)
                {
                    DEBUGMSG(ZONE_ERROR, ("%s: venc state control(stop) fail\n", __FUNCTION__));
                    break;
                }
                strema_0_stop_flag = 0;
                strema_0_start_flag = 1;
                do
                {
                    ret = venc_sample_get_dsp_encode_state(&g_dsp_state);
                    if(ret != SGKS_SUCCESS)
                    {
                        DEBUGMSG(ZONE_ERROR, ("%s: venc get dsp state fail\n", __FUNCTION__));
                        break;
                    }
                }
                while(g_dsp_state.stream_encode_state[stream_id] == 0);
                DEBUGMSG(ZONE_VENC, ("--stream %d start %d--\n\n", stream_id, g_dsp_state.stream_encode_state[stream_id]));
                gettimeofday(&start_tv[stream_id], NULL);
                if(test_time >= START_STOP_TEST_NUM)
                {
                    DEBUGMSG(ZONE_INFO, ("venc pressue test end, stop test.\n"));
                    sgks_mpi_xc9080_sample_venc_stop();
                    break;
                }
                test_time ++;
            }
        }

        stream_id = 1;
        if(gVEncStreams[stream_id].streamFormat.encodeType != 0)
        {
            ret = venc_sample_get_dsp_encode_state(&g_dsp_state);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("%s: venc get dsp state fail\n", __FUNCTION__));
                break;
            }
            if((strema_1_stop_flag == 0) && (g_dsp_state.stream_encode_state[stream_id] == 1))
            {
                DEBUGMSG(ZONE_VENC, ("++stream %d stop++\n", stream_id));
                ret = sgks_mpi_venc_state_control(VENC_STATE_CONTROL_STOP, stream_id);
                if(ret != SGKS_SUCCESS)
                {
                    DEBUGMSG(ZONE_ERROR, ("%s: venc state control(stop) fail\n", __FUNCTION__));
                    break;
                }
                do
                {
                    ret = venc_sample_get_dsp_encode_state(&g_dsp_state);
                    if(ret != SGKS_SUCCESS)
                    {
                        DEBUGMSG(ZONE_ERROR, ("%s: venc get dsp state fail\n", __FUNCTION__));
                        break;
                    }
                }
                while(g_dsp_state.stream_encode_state[stream_id] == 1);
                DEBUGMSG(ZONE_VENC, ("--stream %d stop--\n\n", stream_id));
                strema_1_stop_flag = 1;
                strema_1_start_flag = 0;
            }

            ret = venc_sample_get_dsp_encode_state(&g_dsp_state);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("%s: venc get dsp state fail\n", __FUNCTION__));
                break;
            }
            if((strema_1_start_flag == 0) && (g_dsp_state.stream_encode_state[stream_id] == 0))
            {
                DEBUGMSG(ZONE_VENC, ("++stream %d start++\n", stream_id));
                ret = sgks_mpi_venc_state_control(VENC_STATE_CONTROL_START, stream_id);
                if(ret != SGKS_SUCCESS)
                {
                    DEBUGMSG(ZONE_ERROR, ("%s: venc state control(stop) fail\n", __FUNCTION__));
                    break;
                }
                strema_1_stop_flag = 0;
                strema_1_start_flag = 1;
                do
                {
                    ret = venc_sample_get_dsp_encode_state(&g_dsp_state);
                    if(ret != SGKS_SUCCESS)
                    {
                        DEBUGMSG(ZONE_ERROR, ("%s: venc get dsp state fail\n", __FUNCTION__));
                        break;
                    }
                }
                while(g_dsp_state.stream_encode_state[stream_id] == 0);
                DEBUGMSG(ZONE_VENC, ("--stream %d start %d--\n\n", stream_id, g_dsp_state.stream_encode_state[stream_id]));
                gettimeofday(&start_tv[stream_id], NULL);
            }

        }
    }
    while(1);

    DEBUGMSG(ZONE_INFO, ("%s:thread exit\n", __FUNCTION__));
    pthread_exit(NULL);
}


typedef struct 
{
	char * 	ptr;
	u32		nBytes;
}BUFFER_INFO;

RingBuffer<BUFFER_INFO>gH264Frame(12);

static void *SaveH264Proc(void *params)
{
	FILE *pFile = NULL;
	BUFFER_INFO Info;
	unsigned long filesize,x;
	unsigned char ch ='A';
	char filename[256];
	
	while(1)
	{
		if(!pFile)
		{
			sprintf(filename, "/home/TF/%c.h264", ch++);
			pFile = fopen(filename, "wb");
			filesize = 0;
		}
		if(pFile)
		{
			if(gH264Frame.PopItem(Info))
			{
				x = 0;
				while(x <Info.nBytes)
					x += fwrite(Info.ptr+x, 1,Info.nBytes-x, pFile);
				filesize += Info.nBytes;
				fflush(pFile);
			}
			else
				usleep(100000);
		}
		if(filesize >(1<<31))
		{
			fclose(pFile);
			pFile = NULL;
		}
	}	
}



static void *venc_sample_capture_proccess(void *params)
{
    int ret;
	char *	pBuffer[16];
	int		nBufferIndex = 0;
	int		nBufferBytes = 0;
    sgks_mpi_venc_stream_s *stream = (sgks_mpi_venc_stream_s *)params;
    struct timeval cur_tv;
    u32 second_time = 0, print_flag = 1;
    sgks_encode_stream_s  VideoItem;
    u32 minute_num = 0;
    u32 cur_venc_state[2] = {0xFF, 0xFF};
	BUFFER_INFO	 Info;
	pthread_t caph264;

    DEBUGMSG(ZONE_INFO, ("Create H264 Capture Thread\n"));
    memset(&VideoItem, 0x00, sizeof(sgks_encode_stream_s));
    gettimeofday(&enc_pre_tv, NULL);
    pthread_detach(pthread_self());
    if(params == NULL)
    {
        ret = -1;
        pthread_exit((int *)ret);
    }


	for(int i = 0;i<16;i++)
			pBuffer[i] = (char *)malloc((1<<21)+(1<<20));
#if 0
		pthread_attr_t attr;
		struct sched_param param;
	
		pthread_attr_init(&attr);
		pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
		pthread_attr_setschedpolicy(&attr,SCHED_FIFO);//SCHED_RR
		param.sched_priority = 90;
		pthread_attr_setschedparam(&attr, &param);
	
		if(pthread_create(&caph264, &attr, SaveH264Proc, &param) != 0)
		{
			printf("venc_sample_capture_proccess saveH264 error.\n");		
		}
		else
		{
			printf("venc_sample_capture_proccess create saveH264 pthread 999 ok \n");		
		}
		pthread_attr_destroy(&attr);
#else	
		if(pthread_create(&caph264, NULL, SaveH264Proc,NULL) != 0)
		{
			printf("venc_sample_capture_proccess saveH264 error.\n");		
		}
		else
		{
			printf("venc_sample_capture_proccess create saveH264 pthread ok ...... \n");		
		}
	
#endif



	
    do
    {
        sgks_mpi_dsp_status_s   dsp_state;
        dsp_state.op_mode = SGKS_DSP_UNKNOWN_MODE;
        dsp_state.op_sub_mode = SGKS_DSP_ENC_UNKNOWN_MODE;
        dsp_state.state = SGKS_ENC_UNKNOWN_STATE;

        ret = sgks_mpi_sys_GetDspStats(&dsp_state);
        if(ret != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("Get DSP STATE FAIL\n"));
            ret = SGKS_ERR_VENC_DEVICEERR;
        }

        if((dsp_state.state == SGKS_ENC_BUSY_STATE) && ((dsp_state.stream_encode_state [0] == 1) || ((dsp_state.stream_encode_state [1] == 1))))
        {

            if (sgks_mpi_venc_get_stream(0xff, stream) != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("%s chn:%d failed\n", __FUNCTION__, stream->stream_id));
                continue;
            }
            else
            {
                if(stream->stream_end == 1)
                {
                    //DEBUGMSG(ZONE_VENC, ("%s:get stream end\n", __FUNCTION__));
                    continue;
                }
//#ifdef ENCODE_SAVE_FILE
#if 0
                if(g_save_h264_file_flag == 1)
                {
                    VideoItem.nSize = stream->size;
                    VideoItem.pData = stream->addr;
                    VideoItem.stType = stmVideo;
                    VideoItem.FrameNumber = stream->frame_num;
                    VideoItem.nChannel = stream->stream_id;
                    VideoItem.nFrameType = stream->pic_type;
                    VideoItem.vetEncodeType = (sgks_video_ecode_type_t)gVEncStreams[stream->stream_id].streamFormat.encodeType;
                    //pthread_mutex_lock(&gLock);
                    venc_sample_video_encode_callback(&VideoItem);
                    //pthread_mutex_unlock(&gLock); 
                }
#else
				memcpy(pBuffer[nBufferIndex & 0x0f] + nBufferBytes, stream->addr, stream->size);
				nBufferBytes += stream->size;
				if(nBufferBytes >= (1<<21))
				{
					//Post to another thread to write a file.
					Info.ptr = pBuffer[nBufferIndex & 0x0f];
					Info.nBytes = nBufferBytes;
					//if(!gH264Frame.PushItem(Info))
					{
						
					}
					nBufferBytes = 0;
					nBufferIndex ++;
				}

#endif
            }
#ifdef ENCODE_PARAM_TEST
            gettimeofday(&cur_tv, NULL);
            second_time = cur_tv.tv_sec - enc_pre_tv.tv_sec + 1;
            if(second_time%TEST_TIME_INTERVAL == 0)
            {
                if(print_flag == 0)
                {
                    print_flag = 1;
                    sgks_mpi_sample_venc_param_test();

                }
            }
            else
            {
                print_flag = 0;
            }
#endif
            DEBUGMSG(ZONE_THREAD, ("app:num=%d, id=%d, type=%d, size=0x%x\n", stream->frame_num, stream->stream_id, stream->pic_type, stream->size));
        }
    }
    while(1);
    DEBUGMSG(ZONE_INFO, ("%s:thread exit\n", __FUNCTION__));
    pthread_exit(NULL);
}

void *venc_sample_encode_yuv_to_h264(void *param)
{
//#define YUV_PITCH 1280
//#define YUV_HEIGHT 720
#define YUV_PITCH 1920
#define YUV_HEIGHT 1080
    int ret = SGKS_SUCCESS;
    u32 i = 0;
    int yuvfd;
    char filename[40];
    struct stat fstatbuf;
    sgks_mpi_venc_yuv_enc_s yuvinfo;
    sgks_mpi_venc_enc_buf_s bufinfo;
    u8 *y_addr, *uv_addr;
    u32 rlen;

    //first must get encode buffer memory
    ret = sgks_mpi_venc_map_encode_buf(&bufinfo);
    if(ret != SGKS_SUCCESS)
    {
        DEBUGMSG(ZONE_ERROR, ("gadi_venc_map_encode_buf error\n"));
        return NULL;
    }
    DEBUGMSG(ZONE_VENC, ("bufinfo.addr:0x%x\n", bufinfo.addr));
    DEBUGMSG(ZONE_VENC, ("bufinfo.length:0x%x\n", bufinfo.length));

    y_addr = bufinfo.addr;
    while(1)
    {
        //which do you want to encode? I assume encode yuv file at /home/enc_yuv folder
        sprintf(filename, "/home/enc_yuv/%dx%d_%d.yuv",YUV_PITCH, YUV_HEIGHT, i++);
        yuvfd = open(filename, O_RDONLY);
        if(yuvfd <= 0)
        {
            DEBUGMSG(ZONE_VENC, ("no file to be open, end-%d\n", i));
            sgks_mpi_xc9080_sample_venc_stop();
           return NULL;
        }
        fstat(yuvfd, &fstatbuf);
        if(((u32)y_addr + fstatbuf.st_size) > ((u32)bufinfo.addr + bufinfo.length))
        {
            y_addr = bufinfo.addr;
        }
        rlen = read(yuvfd, y_addr, fstatbuf.st_size);
        if(rlen != fstatbuf.st_size)
        {
            DEBUGMSG(ZONE_ERROR, ("read file:%s length %d:%d error\n", filename, rlen, fstatbuf.st_size));
           return NULL;
        }
        yuvinfo.enc_enable  = 1;//0:means disable encode; 1:means enable encode
        yuvinfo.zoom_enable = 1;
        yuvinfo.area_id = 1;
        yuvinfo.y_addr      = (u32)y_addr;
        yuvinfo.uv_addr     = (u32)(y_addr + YUV_PITCH*YUV_HEIGHT);
        yuvinfo.repeat_nums = 0;
        yuvinfo.pitch       = YUV_PITCH;
        yuvinfo.height      = YUV_HEIGHT;
        //DEBUGMSG(ZONE_VENC, ("yuv->h264: y_addr:0x%x\n", yuvinfo.y_addr));
        //DEBUGMSG(ZONE_VENC, ("yuv->h264: uv_addr:0x%x\n", yuvinfo.uv_addr));
        //DEBUGMSG(ZONE_VENC, ("yuv->h264: zoom_enable:0x%x\n", yuvinfo.zoom_enable));
        //DEBUGMSG(ZONE_VENC, ("yuv->h264: area_id:0x%x\n", yuvinfo.area_id));
        //DEBUGMSG(ZONE_VENC, ("yuv->h264: repeat_nums:%u\n", yuvinfo.repeat_nums));
        //DEBUGMSG(ZONE_VENC, ("yuv->h264: pitch:%u\n", yuvinfo.pitch));
        //DEBUGMSG(ZONE_VENC, ("yuv->h264: height:%u\n\n", yuvinfo.height));
        DEBUGMSG(ZONE_VENC, ("yuv->h264 %s\n", filename));
        ret = sgks_mpi_venc_feed_yuv_to_encode(&yuvinfo);
        if(ret != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_ERROR, ("gadi_venc_feed_yuv_to_encode error\n"));
            return NULL;
        }
        y_addr += fstatbuf.st_size;
    }

	return NULL;
}

static void *venc_sample_encode_yuv_to_jpeg(void *param)
{
//#define YUV_PITCH 1280
//#define YUV_HEIGHT 720
#define YUV_PITCH 1920
#define YUV_HEIGHT 1080

    int ret = SGKS_SUCCESS;
    u32 i;
    int yuvfd;
    char filename[40];
    struct stat fstatbuf;
    u32 rlen;
    u32 dstJpeg, dstSize;
    unsigned char *psrcYuv;

    struct timeval tv1, tv2;

    unsigned int filecount = 0;
    char filedir[64], openfile[64];
    int fd_sav;
    unsigned int wlen;

    umask(~(S_IRWXU|S_IRWXO|S_IRWXG));
    system("rm -rf /home/YuvToJpeg");
    system("mkdir /home/YuvToJpeg");
    strcpy(filedir, "/home/YuvToJpeg");

    DEBUGMSG(ZONE_VENC, ("%s enter\n", __FUNCTION__));
    psrcYuv = (unsigned char *)sgks_mpi_Malloc(0x400000);
    while(1)
    {
        //which do you want to encode? I assume encode yuv file at /home/enc_yuv folder
        sprintf(filename, "/home/enc_yuv/%dx%d_%d.yuv",YUV_PITCH, YUV_HEIGHT, i++);
        yuvfd = open(filename, O_RDONLY);
        if(yuvfd <= 0)
        {
            DEBUGMSG(ZONE_ERROR, ("no file to be open, end-%d\n", i));
            sgks_mpi_xc9080_sample_venc_stop();
            return NULL;
        }
        fstat(yuvfd, &fstatbuf);
        rlen = read(yuvfd, psrcYuv, fstatbuf.st_size);
        if(rlen != fstatbuf.st_size)
        {
            DEBUGMSG(ZONE_VENC, ("read file:%s length %d:%d error\n", filename, rlen, fstatbuf.st_size));
            return NULL;
        }

        gettimeofday(&tv1, NULL);

        {
            sgks_mpi_venc_enc_buf_s bufinfo;
            static int maponce = 0;

            if(maponce == 0)
            {
                maponce = 1;
                ret = sgks_mpi_venc_map_encode_buf(&bufinfo);
                if(ret != SGKS_SUCCESS)
                {
                    DEBUGMSG(ZONE_ERROR, ("gadi_venc_map_encode_buf error\n"));
                    return NULL;
                }
                else
                {
                    DEBUGMSG(ZONE_VENC, ("MAP BUF addr: 0x%x, len: 0x%x\n", bufinfo.addr, bufinfo.length));
                }
            }
            memcpy(bufinfo.addr, psrcYuv, YUV_PITCH*YUV_HEIGHT*3/2);
            if (60 != gVEncStreams[1].mjpegConf.quality)
            {
                // dos qos operation
                gVEncStreams[1].mjpegConf.quality = 60;
                if(SGKS_SUCCESS != sgks_mpi_venc_set_config(VENC_CONFIG_TYPE_MJPEG, (void *)&gVEncStreams[1].mjpegConf))
                {
                    DEBUGMSG(ZONE_ERROR, ("sgks_mpi_venc_set_config error, steamId=1\n"));
                   // return SGKS_ERR_VENC_STATEERROR;
                   return NULL;
                }
                DEBUGMSG(ZONE_VENC, ("sgks_mpi_venc_set_config ok\n"));
            }

            ret = sgks_mpi_venc_yuv_to_jpeg((u32)bufinfo.addr, YUV_PITCH, YUV_HEIGHT, &dstJpeg, &dstSize);
            if(ret != SGKS_SUCCESS)
            {
                DEBUGMSG(ZONE_ERROR, ("sgks_mpi_venc_yuv_to_jpeg error\n"));
                //return SGKS_ERR_VENC_STATEERROR;
                return NULL;
            }
        }

        gettimeofday(&tv2, NULL);
        DEBUGMSG(ZONE_VENC, ("********time: %dms\n", tv2.tv_sec*1000 + tv2.tv_usec/1000 - tv1.tv_sec*1000 - tv1.tv_usec/1000));

        //save file
        sprintf(openfile, "%s/jpeg_%d.jpeg", filedir, filecount++);
        fd_sav = open(openfile, O_CREAT|O_RDWR, (S_IRWXU|S_IRWXO|S_IRWXG));
        if(fd_sav < 0)
        {
            DEBUGMSG(ZONE_ERROR, ("error: open file:%s error:%d\n", openfile, fd_sav));
        }
        wlen = write(fd_sav, (const void *)dstJpeg, dstSize);
        if(wlen != dstSize)
        {
            DEBUGMSG(ZONE_ERROR, ("error: write %s wlen:%u != dstSize:%u\n", openfile, wlen, dstSize));
             
        }
        close(fd_sav);
    }

	return NULL;
	
}

static void *venc_vi_switch_thread(void *param)
{
    int ret = SGKS_SUCCESS;
    while(1)
    {
        DEBUGMSG(ZONE_VENC, ("vi switch 0...\n"));
        ret = sgks_mpi_sys_ViBindVo(0, 0);
        if (ret != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_VENC, ("switch fail\n"));
        }
        usleep(5000*1000);
        DEBUGMSG(ZONE_VENC, ("vi switch 1...\n"));
        ret = sgks_mpi_sys_ViBindVo(1, 0);
        if (ret != SGKS_SUCCESS)
        {
            DEBUGMSG(ZONE_VENC, ("switch fail\n"));
        }        
        usleep(5000*1000);
    }

	return NULL;
	
}

int sgks_mpi_sample_venc_start_stop_pressure_test(void)
{
    int ret = SGKS_SUCCESS;
    sgks_mpi_venc_stream_s stream;
    pthread_t PreTest;
    if(pthread_create(&PreTest, NULL, venc_sample_pressure_test_proccess, &stream) != 0)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: thread create fail\n", __FUNCTION__));
        ret = SGKS_ERR_VENC_STATEERROR;
    }
    return ret;
}

int sgks_mpi_sample_venc_stream_capture_test()
{
    int ret = SGKS_SUCCESS;

    sgks_mpi_venc_stream_s stream;
    pthread_mutex_init(&gLock, NULL);

    pthread_attr_t attr;
    struct sched_param param;

    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&attr,SCHED_FIFO);//SCHED_RR
    param.sched_priority = 90;
    pthread_attr_setschedparam(&attr, &param);

    DEBUGMSG(ZONE_INFO, ("venc stream capture test start\n"));
    pthread_t CaptureH264;
    if(pthread_create(&CaptureH264, &attr, venc_sample_capture_proccess, &stream) != 0)
    {
        printf("snapshot stop MJPEG stream.\n");
        ret = SGKS_ERR_VENC_STATEERROR;
    }
    pthread_attr_destroy(&attr);
    return ret;
}

int sgks_mpi_sample_venc_vi_switch_test()
{
   int ret = SGKS_SUCCESS;
    pthread_t ViTestThread;
    if(pthread_create(&ViTestThread, NULL, venc_vi_switch_thread, NULL) != 0)
    {
        DEBUGMSG(ZONE_ERROR, ("%s: thread create fail\n", __FUNCTION__));
        ret = SGKS_ERR_VENC_STATEERROR;
    } 
    return ret;
}

int sgks_mpi_xc9080_sample_venc_test(int venc_test_mode)
{

    int ret = SGKS_SUCCESS;
    pthread_t YuvEncode;
    if((venc_test_mode != YUV_DATA_MJPEG_ENCODE_TEST))
    {
        ret = sgks_mpi_sample_venc_stream_capture_test();
        if (ret != SGKS_SUCCESS)
            return ret;
    }

    if((venc_test_mode == YUV_DATA_H264_ENCODE_TEST))
    {
        if(pthread_create(&YuvEncode, NULL, venc_sample_encode_yuv_to_h264, NULL) != 0)
        {
            DEBUGMSG(ZONE_ERROR, ("YUV ENCODE THREAD CREATE FAIL\n"));
            ret = SGKS_ERR_VENC_STATEERROR;
        }
        return ret;

    }

    if((venc_test_mode == YUV_DATA_MJPEG_ENCODE_TEST))
    {
        if(pthread_create(&YuvEncode, NULL, venc_sample_encode_yuv_to_jpeg, NULL) != 0)
        {
            DEBUGMSG(ZONE_ERROR, ("YUV ENCODE THREAD CREATE FAIL\n"));
            ret = SGKS_ERR_VENC_STATEERROR;
        }
        return ret;

    }

    if(venc_test_mode == START_STOP_PRESSURE_TEST)
    {
        ret = sgks_mpi_sample_venc_start_stop_pressure_test();
        if (ret != SGKS_SUCCESS)
            return ret;
    }
    
#ifdef ENABLE_VI_SWITCH_TESE
    ret = sgks_mpi_sample_venc_vi_switch_test();
#endif
    return ret;
}
